import { makeAutoObservable } from 'mobx'
import type { IFormStatisticArrItem, ILot_obj } from './interface'
import { createTrainings, getStatisticInfo, teachertLogin } from './api'
import { setCookie, setSessionStorage } from '@/storage'

const defaultResult = [
    {
        user_id: 's1',
        name: '',
        price: '',
        avatar: '',
        height: 60,
    },
    {
        user_id: 's2',
        name: '',
        price: '',
        avatar: '',
        height: 60,
    },
    {
        user_id: 's3',
        name: '',
        price: '',
        avatar: '',
        height: 60,
    },
    {
        user_id: 's4',
        name: '',
        price: '',
        avatar: '',
        height: 60,
    },
    {
        user_id: 's5',
        name: '',
        price: '',
        avatar: '',
        height: 60,
    },
]
class Store {
    /** 用户登录信息 */
    userInfo: any = {}
    /** 实训游戏id */
    auction_record_id: number = 0
    /** 是否全屏 */
    isFullScreen: boolean = false
    /** 拍卖表统计 */
    form_statistic_arr: IFormStatisticArrItem[] = []
    /** 竞拍初始化数据 */
    binding_info: any = {
        binding_result: defaultResult,
        bidding_price_avg: 0,
        bidding_student_num: 0,
    }
    /** 拍品信息 */
    lot_obj: ILot_obj | Record<string, any> = {}

    /** 统计结果数据 */
    statisticInfo = {
        statistic_arr: [],
        form_statistic_arr: [],
    }

    constructor() {
        makeAutoObservable(this)
    }

    /** 创建游戏 */
    createAuction = (student_num: number) => {
        createTrainings({ student_num, training_id: '1' }).then(res => {
            this.auction_record_id = res?.data?.id
        })
    }

    /** 教师端快捷登录 */
    teacherquickLogin = (data: any) => {
        return teachertLogin(data).then(res => {
            console.log(res)
            this.userInfo = res.data
            setCookie('TOKEN', res.data.access_token)
            setSessionStorage('TEACHER_USER_INFO', res.data)
        })
    }

    /** 初始化下一件拍卖数据 */
    initBindingInfo = () => {
        this.binding_info = {
            ...this.binding_info,
            binding_result: defaultResult,
        }
    }

    /** 设置用户数据 */
    setUserInfo = (data: any) => {
        this.userInfo = data
    }

    /** 修改全屏状态 */
    setFullScreen = (val: boolean) => {
        this.isFullScreen = val
    }

    /** 设置数据 */
    setData = (key: 'form_statistic_arr' | 'binding_info' | 'lot_obj', value: any) => {
        this[key] = value
    }

    /** 获取统计结果数据 */
    getTrainingStatisticInfo = (record_id: string) => {
        getStatisticInfo({ record_id }).then(res => {
            this.statisticInfo = res.data
        })
    }
}

const store = new Store()
export default store
